package com.smartmedical.auth.doctor.controller;

import com.smartmedical.auth.doctor.form.PatientUserBody;
import com.smartmedical.auth.doctor.service.PatientUserService;

import com.smartmedical.auth.form.PatientUserVo;

import com.smartmedical.common.core.domain.HealthR;
import com.smartmedical.common.core.domain.R;
import com.smartmedical.common.security.service.TokenService;
import com.smartmedical.system.api.domain.PatientUsers;
import com.smartmedical.system.api.domain.SysUser;
import com.smartmedical.system.api.model.LoginUser;
import com.smartmedical.system.api.model.PatientUserJwt;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * @BelongsProject: smart-medical
 * @BelongsPackage: com.smartmedical.auth.controller
 * @ClassName: PatientTokenController
 * @Author: zyn
 * @CreateTime: 2024-01-08  18:08
 * @Description: TODO
 */
@RestController
@RequestMapping("user/v1")
public class PatientTokenController {


    @Autowired
    private PatientUserService patientUserService;

    @Autowired
    private TokenService tokenService;

    /**
     * 登录接口
     *
     * @param email 邮箱
     * @param pwd   密码
     * @return 登录结果
     */



    @PostMapping("/login")
    public HealthR<?> logins(@RequestParam String email, @RequestParam String pwd)
    {
        //用户登录
        PatientUserBody login = patientUserService.login(email, pwd);
        LoginUser loginUser = new LoginUser();
        loginUser.setUserid(login.getId());
        loginUser.setUsername(login.getEmail());
        SysUser sysUser = new SysUser();
        sysUser.setUserName(login.getEmail());
        sysUser.setUserId(login.getId());
        loginUser.setSysUser(sysUser);

        Map<String, Object> tokens = tokenService.createToken(loginUser);
        String sessionid = (String) tokens.get("access_token");
        //赋值
        PatientUserVo patientUserVo = createPatientUserVo(login, sessionid);
        // 获取登录token
        return HealthR.ok(patientUserVo,"登录成功");
    }

    private PatientUserVo createPatientUserVo(PatientUserBody login, String sessionid) {

        PatientUserVo patientUserVo = new PatientUserVo();
        patientUserVo.setUserId(login.getId());
        patientUserVo.setSessionId(sessionid);
        patientUserVo.setNickName(login.getNickName());
        patientUserVo.setUserName(login.getUserName());
        patientUserVo.setJiGuangPwd(login.getPwd());
        patientUserVo.setHeadPic(login.getHeadPic());
        patientUserVo.setSex(login.getSex());
        patientUserVo.setAge(login.getAge());
        patientUserVo.setHeight(login.getHeight());
        patientUserVo.setWeight(login.getWeight());
        patientUserVo.setEmail(login.getEmail());

        patientUserVo.setWhetherBingWeChat(1);
        patientUserVo.setInvitationCode(login.getInvitationCode());
        patientUserVo.setFaceFlag(1);

        return patientUserVo;
    }


}
